import "miniprogram-api-typings";
import { buildIcon } from "./icon";
export const preloadImage = (arr?: Array<string>, isIcon: boolean = true) => {
	// const preloadList: Ref<Array<string>> = ref([]);
	const preloadList = ref<Array<string>>([]);
	if ((arr || []).length > 0) {
		preloadList.value = arr || [];
	}
	/* #ifdef MP-WEIXIN */
	wx.preloadAssets({
		data: preloadList.value.map((item: string) => {
			return {
				type: "image",
				src: isIcon ? buildIcon(item) : item,
			};
		}),
	});
	/* #endif */
	/* #ifndef MP-WEIXIN */
	preloadList.value.map((item: string) => {
		uni.getImageInfo({ src: isIcon ? buildIcon(item) : item });
	});
	/* #endif */
};

/**
 * 判断当前是否在微信浏览器内
 */
export const checkBrowersIsWechat = () => {
	let ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("micromessenger") !== -1) {
		return true;
	} else {
		return false;
	}
};
/**
 * 判断当前是Android还是IOS
 */
export const deviceJudgment = () => {
	let u = navigator.userAgent;
	const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //android终端或者uc浏览器
	const isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	return { isAndroid, isIos };
};

export const switchTabOrNavigate = (url: string) => {
	const tabList = [
		"/pages/shop/index",
		"/pages/index/index",
		"/pages/mine/index",
		"/pages/activity/index",
	];
	if (tabList.includes(url)) {
		uni.switchTab({ url });
	} else {
		uni.navigateTo({ url });
	}
};
